package com.office.automate.dataaccess.impl.administration;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.office.automate.dataaccess.def.administration.BranchDao;
import com.office.automate.dataaccess.framework.DataAccessSessionFactory;
import com.office.automate.exception.administration.BranchExceptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.administration.Branch;

public class BranchDaoImplementer implements BranchDao {

	private static SessionFactory sessionFactory = null;
	private Session session = null;

	static {
		sessionFactory = DataAccessSessionFactory.getSessionFactory();
	}

	public BranchDaoImplementer() {
		session = sessionFactory.openSession();		
	}

	public int getAvailableBranchDetailRecordCount() throws FrontierVisibleException {

		int recordCount = 0;
		try {
			 String SQL_QUERY ="select count(*) from Branch branch";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 recordCount = ((Long) queryResult.list().get(0)).intValue();
		} catch(Exception e) {
			throw new FrontierVisibleException(BranchExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		}
		return recordCount;
	}

	@SuppressWarnings("unchecked")
	public List<Branch> getAvailableBranchesPage(int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {

		List<Branch> aBranchList = null;
		try {
			 String SQL_QUERY ="from Branch branch order by branch.branchId asc";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 queryResult.setFirstResult(aStartingIndex);
			 queryResult.setMaxResults(aNumberOfRecords);
			 aBranchList = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(BranchExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return aBranchList;
	}

	public Branch persistBranchDetails(Branch aBranch) throws FrontierVisibleException {

		try {
			Transaction transaction = session.beginTransaction();
			session.save(aBranch);
			transaction.commit();
		} catch(Exception e) {
			throw new FrontierVisibleException(BranchExceptionKeys.FAIL_TO_PERSIST_BRANCH, e);
		} finally {
			session.flush();
			session.close();
		}
		return aBranch;
	}

	public void deleteBranch(Branch aBranch) throws FrontierVisibleException {
		try {
			session.beginTransaction();   
			session.delete(aBranch);  
			session.getTransaction().commit();  
		} catch(Exception e) {
			throw new FrontierVisibleException(BranchExceptionKeys.ERROR_OCCORRED_DURING_DELETION, e);
		} finally {
			session.flush();
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Branch> getAvailableBranches() throws FrontierVisibleException {

		List<Branch> availableBranchs = null;
		try {
			 String SQL_QUERY ="from Branch branch";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 availableBranchs = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(BranchExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return availableBranchs;
	}
}